package com.fance.aspect;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

/**
 * @author fance
 * @date 18-5-3 下午4:24
 */
@Aspect
@Component
public class LogAspect {

    private static final Logger logger = LoggerFactory.getLogger(LogAspect.class);

    /**
     * 在方法前拦截,获取方法的参数.控制器中所有方法.
     * @param joinPoint
     */
    @Before("execution(* com.fance.controller.*Controller.*(..))")
    public void beforeMethod(JoinPoint joinPoint) {
        StringBuilder sb = new StringBuilder();
        for (Object arg: joinPoint.getArgs()
             ) {
            sb.append("arg: " + arg.toString() + "|");
        }
        logger.info("before method: " + sb.toString());
    }

    /**
     * 在方法后拦截,控制器中所有方法.
     * @param joinPoint
     */
    @After("execution(* com.fance.controller.*Controller.*(..))")
    public void afterMethod(JoinPoint joinPoint) {
        logger.info("after method: ");
    }

}
